package com.paradogs.framework.misc;

import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.paradogs.common.utils.ProtoUtils;
import lombok.Data;

/**
 * @author: yumi
 * @date: 2023/12/28  16:19
 * @Description: 回复消息
 */
@Data
public class GC<T> {

    public static final String DEFAULT_SUCCESS_MESSAGE = null; // "ok"
    public static final String DEFAULT_FAIL_MESSAGE = "fail"; // "fail"

    @Protobuf
    private Integer code; // 1 成功，0 失败
    @Protobuf
    private String message;
    @Protobuf
    private byte[] data;

    private T objData;

    public GC() {}
    public GC(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.objData = data;
        this.data = ProtoUtils.encode(data);
    }

    public static GC ok() {
        return new GC(1, DEFAULT_SUCCESS_MESSAGE, null);
    }

    public static <T> GC ok(T data) {
        return new GC(1, DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static GC fail() {
        return fail(DEFAULT_FAIL_MESSAGE);
    }

    public static GC fail(String message) {
        return new GC(0, message, null);
    }

    public static GC fail(int code, String message) {
        return new GC(code, message, null);
    }

}
